﻿<%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<%@ Import Namespace="Equipment.Business" %>
<%@ Import Namespace="Equipment.DataAccess" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    LinkRequirements
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="CurrentMenuIDContent" runat="server" >assetMenu</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="HeadContent" runat="server">
    <link rel="stylesheet" type="text/css" href="../../Content/anytime.css" />
    <link rel="stylesheet" type="text/css" href="../../Content/jquery-ui-1.8.16.custom.css" />
    <script type="text/javascript" src="../../Scripts/site.js"></script>
    <script type="text/javascript" src="../../Scripts/jquery-ui-1.8.16.custom.min.js"></script>
    <script type="text/javascript" src="../../Scripts/anytime.js"></script>
    <!--<script type="text/javascript" src="../../Scripts/anytimetz.js"></script>-->
    <script type="text/javascript" >
    AnyTime.utcLabel = []; 

    <% 
    Dim prevMinutes As Integer = -1000
    Dim minuteLabels As New List(Of String)
    For Each item In ViewData("TimeZoneList")
    Dim minutes As Integer = item.BaseUtcOffset.TotalMinutes
    If prevMinutes <> minutes Then
        If minuteLabels.Count = 0 Then
          prevMinutes = minutes
          minuteLabels.Add(item.DaylightName)
        Else %>
        AnyTime.utcLabel[<%: prevMinutes%>]=['<%= String.Join("', '",minuteLabels)%>'];
    <% 
            prevMinutes = minutes
            minuteLabels.Clear()
            minuteLabels.Add(item.DaylightName)
        End If
    Else
        minuteLabels.Add(item.DaylightName)
    End If
    %>
    <% Next%>
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>
        Asset Link Requirements</h2>
    <p>
        <%= Html.ValidationSummary(False) %>
        <% Using Html.BeginForm()%>
        <br />
        <h3>
            Asset:&nbsp;<%= Html.Encode(ViewData("AssetName"))%></h3>
    </p>
    <table>
        <tr>
        <td class="top">Link Requirements:</td>
            <td align="left">
                <table class="data-table">
                    <thead>
                        <tr>
                            <th>
                                &nbsp;
                            </th>
                            <th>
                                Select Requirements
                            </th>
                            <th>
                                Start From
                            </th>
                            <th width="45px">
                                Group
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <% For Each item In ViewData("Requirements")%>
                        <tr>
                            <td>
                                <%: Html.CheckBox(String.Concat("chbxRequirement_", item.ID), ViewData(String.Format("checkedRequirement_{0}", item.ID)) IsNot Nothing, New With {.onclick = String.Format("changeGroupImage({0});", item.ID)})%>
                            </td>
                            <td>
                                <%: item.Name%>
                            </td>
                            <td>
                                <%= Html.TextBox(String.Concat("txtDate_", item.ID), If(ViewData(String.Format("startDate_{0}", item.ID)), TimeZoneManager.ConvertToUserTimeStringFor(DateTime.UtcNow, False)), New With {.class = "text wide-input", .onkeydown = "return false;"})%>
                                <img class="ui-datepicker-trigger calendar-button" src="../../Content/img/calendar.gif" alt="..." title="..." onclick="$('#<%= String.Concat("txtDate_", item.ID) %>').click();"/>
                                <%= Html.ValidationMessage(String.Format("startDateErr_{0}", item.ID))%>
                            </td>
                            <td width="40px" align="right">
                            <%If ViewData(String.Format("checkedRequirement_{0}", item.ID)) Is Nothing Then%>
                            <img id='<%: String.Concat("groupImg_", item.ID)%>' src="../../Content/img/role_access_gray.png" class="calendar-button"/>
                            <%= Html.Hidden(String.Format("hfReqHasEmptySelection_{0}", item.ID), "true")%>
                            <% Else%>

                            <%If ViewData(String.Format("hfReqStatus_{0}", item.ID)) <> "true" Or (ViewData(String.Format("hfReviewReqStatus_{0}", item.ID)) <> "true" AndAlso ViewData(String.Format("RequiredReviewType_{0}", item.ID)) IsNot Nothing) Then%>
                                <%= Html.Hidden(String.Format("hfReqHasEmptySelection_{0}", item.ID), "true")%>
                                <img id='<%: String.Concat("groupImg_", item.ID)%>' src="../../Content/img/role_access_warning.png" alt="Select role..." title="Select role..." onclick="changeGroups(<%: item.ID %>);" class="calendar-button"/>
                            <% Else%>
                                <%= Html.Hidden(String.Format("hfReqHasEmptySelection_{0}", item.ID), "false")%>
                                <img id='<%: String.Concat("groupImg_", item.ID)%>' src="../../Content/img/role_access.png" alt="Select role..." title="Select role..." onclick="changeGroups(<%: item.ID %>);" class="calendar-button"/>
                            <% End If%>
                            <% End If%>

                            <%= Html.Hidden(String.Format("hfReqStatus_{0}", item.ID), ViewData(String.Format("hfReqStatus_{0}", item.ID)))%>
                            <%= Html.Hidden(String.Format("hfReqToChange_{0}", item.ID), "false")%>
                            <%= Html.ValidationMessage(String.Format("roleErr_{0}", item.ID))%>
                            <%  Html.RenderPartial("~/Views/Asset/Controls/GroupDialog.ascx", item)%>
                            </td>
                        </tr>
                        <% Next%>
                    </tbody>
                </table>
            </td>
        </tr>
        <tr>
        <td>&nbsp;</td>
            <td>
                <input type="submit" class="button middle-button right" value="Save" onclick="return validateSelection();" />
            </td>
        </tr>
    </table>
    <% End Using%>
    <div>
        <%: Html.ActionLink("Back to List", "Index") %>
    </div>
    <script type="text/javascript">
      $(function() {
          $('.wide-input').AnyTime_picker(
              { format: "%Y-%m-%d %h:%i %p %:",
                formatUtcOffset: "%: (%@)" } );
        });

        function changeGroupImage(id) {
            var isChecked = $("#chbxRequirement_" + id).is(':checked');
            var isDefined = $("#hfReqStatus_" + id).val() === "true";
            var isReviewDefined = $("#hfReviewReqStatus_" + id).val() === "true";
            var reviewType = $("#ReviewType_" + id).val();
            if (isChecked) {
                var btnFunc = "changeGroups(" + id + ");";
                var groupImg = $("#groupImg_" + id);
                groupImg[0].onclick = function () { eval(btnFunc); return false; };
                if (isDefined && (isReviewDefined || reviewType === "NotRequired")) {
                    groupImg.attr("src", "../../Content/img/role_access.png");
                    $("#hfReqHasEmptySelection_" + id).val("false");
                }
                else {
                    groupImg.attr("src", "../../Content/img/role_access_warning.png");
                    $("#hfReqHasEmptySelection_" + id).val("true");
                }
            }
            else {
                var groupImg = $("#groupImg_" + id);
                groupImg[0].onclick = function () { return false; };
                groupImg.attr("src", "../../Content/img/role_access_gray.png");
                $("#hfReqHasEmptySelection_" + id).val("false");
            }
        }

    function closeDialog(id) {
        $("#groupDialog_" + id).dialog("close");
    }

    function changeGroups(id) {
        $("#groupDialog_" + id).dialog({
            modal: true,
            width: 700,
            height: 500,
            zIndex: 100
        });
        $("#groupDialog_" + id).parent().appendTo($("form:first"));
    }

    function saveGroups(id) {
        var checkedGroupCount = $('input:checkbox[id^="cbxGroup_req' + id + '"]:checked').length;
        var checkedReviewGroupCount = $('input:checkbox[id^="cbxReviewGroup_req' + id + '"]:checked').length;
        var reviewType = $("#ReviewType_" + id).val();

        if (checkedGroupCount != 0 && (checkedReviewGroupCount != 0 || reviewType === "NotRequired")) {
            $("#groupImg_" + id).attr("src", "../../Content/img/role_access.png");
            $("#hfReqToChange_" + id).val("true");
            $("#hfReqHasEmptySelection_" + id).val("false");
            $("#hfReqStatus_" + id).val("true");
        }
        else {
            $("#groupImg_" + id).attr("src", "../../Content/img/role_access_warning.png");
            $("#hfReqToChange_" + id).val("false");
            $("#hfReqHasEmptySelection_" + id).val("true");
        }

        closeDialog(id);
    }

    function validateSelection() {

        var cbxList = $('input:checkbox[id^="chbxRequirement_"]:checked');
        if (cbxList.length > 0) {
            for (var i = 0; i < cbxList.length; i++) {
                var id = cbxList[i].id.split("_")[1];
                var markerValue = $("#hfReqHasEmptySelection_" + id).val();
                var reviewType = $("#ReviewType_" + id).val();
                if (markerValue === "true" && reviewType != "NotRequired") {
                    alert("At least one of the Review groups has to be selected.");
                    return false;
                }
                if (markerValue === "true") {
                    alert("At least one of the user groups has to be assigned to asset requirement.");
                    return false;
                }

            }
        }
        return true;
    }


   </script> 
</asp:Content>
